<?php

namespace App\Admin\Actions\UserAction;

use Encore\Admin\Facades\Admin;
use Encore\Admin\Actions\RowAction;
use Illuminate\Database\Eloquent\Model;

class DismissPayCode extends RowAction
{
    public $name = '交易密码解除';
    
    public function handle(Model $model)
    {
	    if (!in_array(Admin::user()->id, [1,3])) {
		    return $this->response()->error('无权操作')->refresh();
	    }
	    
	    if (empty($model->pay_code)) {
		    return $this->response()->error('尚未设置交易密码')->refresh();
	    }
	
	    $model->update(['pay_code' => null]);
	    $model->resetPayCodeTry();
	    
	    return $this->response()->success('交易密码解除成功')->refresh();
    }
	
	public function dialog()
	{
		$this->confirm('确定解除当前用户的交易密码?');
	}
}
